package com.biglead.mini.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biglead.common.pojo.CommonResult;
import com.biglead.mini.pojo.ConfirmOrderResult;
import com.biglead.mini.pojo.OmsOrderDetail;
import com.biglead.mini.pojo.OrderParam;
import com.biglead.mini.service.OmsPortalOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 订单管理Controller
 */
@Controller
@Api(tags = "OmsPortalOrderController")

@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OmsPortalOrderService omsPortalOrderService;
    @ApiOperation("立即购买下单")
    @RequestMapping(value = "/confirmOrder", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ConfirmOrderResult> confirmOrder(@RequestHeader Long userId, @RequestBody List<Long> cartIds) {
        ConfirmOrderResult confirmOrderResult = omsPortalOrderService.generateConfirmOrder(userId, cartIds);
        return CommonResult.success(confirmOrderResult);
    }

    @ApiOperation("根据购物车信息生成确认单")
    @RequestMapping(value = "/generateConfirmOrder", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ConfirmOrderResult> generateConfirmOrder(@RequestHeader Long userId, @RequestBody List<Long> cartIds) {
        ConfirmOrderResult confirmOrderResult = omsPortalOrderService.generateConfirmOrder(userId, cartIds);
        return CommonResult.success(confirmOrderResult);
    }

    @ApiOperation("根据购物车信息生成订单")
    @RequestMapping(value = "/generateOrder", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult generateOrder(@RequestHeader Long userId, @RequestBody OrderParam orderParam) {
        Map<String, Object> result = omsPortalOrderService.generateOrder(userId, orderParam);
        return CommonResult.success(result, "下单成功");
    }

    @ApiOperation("按状态分页获取用户订单列表")
    @ApiImplicitParam(name = "status", value = "订单状态：-1->全部；0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭",
            defaultValue = "-1", allowableValues = "-1,0,1,2,3,4", paramType = "query", dataType = "int")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<OmsOrderDetail>> list(
            @RequestHeader Long userId,
            @RequestParam Integer status,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<OmsOrderDetail> orderPage = omsPortalOrderService.listPage(userId,status, pageNum, pageSize);
        return CommonResult.success(orderPage);
    }
    @ApiOperation("根据ID获取订单详情")
    @RequestMapping(value = "/detail/{orderId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<OmsOrderDetail> detail(@PathVariable Long orderId) {
        OmsOrderDetail orderDetail = omsPortalOrderService.detail(orderId);
        return CommonResult.success(orderDetail);
    }

    @ApiOperation("用户取消订单")
    @RequestMapping(value = "/cancelUserOrder", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult cancelUserOrder(Long orderId) {
        omsPortalOrderService.cancelOrder(orderId);
        return CommonResult.success(null);
    }

}
